// TerrainMask.cpp: implementation of the CTerrainMask class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "fxtool.h"
#include <stdlib.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <d3dx9.h>
#include <D3D9.h>

#include "dxutil.h"
#include "d3dutil.h"
#include "d3dfont.h"
#include "D3DEnumeration.h"
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"

//#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include <AniContainer.h>
#include "ToolCamera.h"
#include <AniContainer.h>
#include "Effect.h"
#include <AniContainer.h>
#include "FXDialog.h"
#include <AniContainer.h>
#include "D3dModel.h"
#include "D3dAddon.h"
#include "VShadowVolume.h"
#include "TerrainMask.h"
#include "ToolApp.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

extern ToolApp* g_Draw;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTerrainMask::CTerrainMask()
{
	for (int i=0;i<MAX_LAYER;i++)layer[i]=NULL;
}

CTerrainMask::~CTerrainMask()
{

}

void CTerrainMask::Init()
{
	HRESULT hr;
	LPDIRECT3DTEXTURE9 temptex;
	char fname[80];
	for (int i=0;i<9;i++)
	{
		sprintf(fname,"MASK-LAYER_%d.d2s",i);
		D3DXIMAGE_INFO			imageinfo;
		D3DXGetImageInfoFromFile( fname, &imageinfo );
		D3DXCreateTextureFromFileEx( g_Draw->m_pd3dDevice, fname, 
			imageinfo.Width, imageinfo.Height, imageinfo.MipLevels, 0,
			imageinfo.Format, D3DPOOL_MANAGED, D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR , 
				D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR, 0, NULL, NULL,  &temptex) ;
	    g_Draw->m_pd3dDevice->CreateTexture(imageinfo.Width, imageinfo.Height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &orgmask[i] ,NULL);


		LPDIRECT3DSURFACE9 psurfSrc = NULL;
		LPDIRECT3DSURFACE9 psurfDest = NULL;

		hr = temptex->GetSurfaceLevel( i, &psurfSrc );
		hr = orgmask[i]->GetSurfaceLevel( i, &psurfDest);
		hr = D3DXLoadSurfaceFromSurface(psurfDest, NULL, NULL, psurfSrc, NULL, NULL, D3DX_FILTER_TRIANGLE, 0);
		SAFE_RELEASE(psurfSrc);
		SAFE_RELEASE(psurfDest);							



		D3DLOCKED_RECT locked;
		hr=orgmask[i]->LockRect(0,&locked,NULL,D3DLOCK_DISCARD);
		int twidth=locked.Pitch;
		DWORD *bits=(DWORD *)locked.pBits;
		orgmask[i]->UnlockRect(0);
		temptex->Release();

	}
}
